package com.example.zhu.controller;

import com.example.zhu.entity.Orders;
import com.example.zhu.result.Result;
import com.example.zhu.service.HelperService;
import com.example.zhu.service.OrderService;
import com.example.zhu.utils.JwtUtil;
import com.example.zhu.vo.AddressVO;
import com.example.zhu.vo.HistoryOrderVO;
import com.example.zhu.vo.LoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/user/helper")
public class HelperController {


    @Autowired
    private HelperService helperService;

    @Autowired
    private OrderService orderService;

    /**
     * @param key
     * @return
     */
    @GetMapping("/login")
    public Result<LoginVO> login(@RequestParam String key){

        log.info("用户登录,{}",key);

        Integer type = helperService.login(key);

        if(type==-1){
            return Result.error("密钥错误");
        }

        if(type==0||type==1){
            Map<String, Object> claims = new HashMap<>();
            claims.put("key", key);
            String token = JwtUtil.createJWT(
                    "acnaksjd",
                    2L*60*60*1000,
                    claims);
            LoginVO loginVO = LoginVO.builder()
                    .token(token)
                    .type(type)
                    .build();
            return Result.success(loginVO);
        }
        return Result.error("服务器错误");
    }

    /**
     * 查询某区域已接订单
     */
    @GetMapping("/getAll")
    public Result<HistoryOrderVO> selectAllTakes(){
        HistoryOrderVO historyOrderVO = orderService.selectAll();
        return Result.success(historyOrderVO);
    }

//    public Result<>

    /**
     * 查询某区域订单
     */
    @GetMapping("/getpath")
    public Result<AddressVO> getpath(String path){
        log.info("历史订单查询");
        AddressVO addressVO = orderService.selectAllOrders(path);
        return Result.success(addressVO);
    }


    /**
     * 已取件
     * @param id
     * @return
     */
    @Transactional
    @PutMapping("/take/{id}")
    public Result take(@PathVariable Integer id){
        log.info("当前订单id:{}",id);
        return orderService.take(id, Orders.RECEIVED);
    }

    /**
     * 已送达
     * @param id
     * @return
     */
    @Transactional
    @PutMapping("/arrive/{id}")
    public Result arrive(@PathVariable Integer id,@RequestBody MultipartFile image){
        log.info("当前订单id:{}",id);
        if(image!=null){
            orderService.image(id, image);
        }
        return orderService.arrive(id,Orders.REACHED);
    }

    @GetMapping("/download")
    public void download(String url,  HttpServletResponse response){
        orderService.download(url,response);
    }

    @PutMapping("/difficult/{id}")
    public Result fail(@PathVariable Integer id){
        return orderService.fail(id);
    }

}
